package com.liu.netty.demo.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Date;

/**
 * @program: https://www.joinsunsoft.com
 * @description:
 * @author: LiuYong
 * @email: david.liu@ginghan.com
 * @create: 2022/3/23 10:14
 */

@Slf4j
public class ClientDemoChannelHandler extends SimpleChannelInboundHandler<String> {
    public ClientDemoChannelHandler(String name){
        this.name = name;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        InetSocketAddress address = (InetSocketAddress) (ctx.channel().localAddress());
        ctx.writeAndFlush( "[" + address +"]");
        super.channelActive(ctx);
    }

    private String name;
    private InetSocketAddress address;

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
        log.error("================== Receive message from server: {}", msg);


        ctx.writeAndFlush(name + "["+address+"]" + msg + "<>" + new Date());
    }
}
